package com.example.myfinalhomework;
import android.content.Intent;
import android.content.SharedPreferences;
import android.os.Bundle;
import android.util.Log;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.Toast;
import androidx.appcompat.app.AppCompatActivity;
import android.view.View;
import android.view.View.OnClickListener;



public class MainActivity extends AppCompatActivity {

     EditText usernameEditText;
     EditText passwordEditText;
     Button loginButton;
     private static final String TAG = "MainActivity";

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);

        loginButton=(Button)findViewById(R.id.loginButton);
        usernameEditText = findViewById(R.id.username);
        passwordEditText = findViewById(R.id.password);

    }
    public void B1(View v){
        Log.i(TAG,"B1");
        Log.i(TAG,"成功");
        String username = usernameEditText.getText().toString();
        String password = passwordEditText.getText().toString();


        SharedPreferences sharedPreferences = getSharedPreferences("users", MODE_PRIVATE);
        String storedPassword = sharedPreferences.getString(username, null);

        if (storedPassword != null && storedPassword.equals(password)) {
            Toast.makeText(MainActivity.this, "登录成功", Toast.LENGTH_SHORT).show();
            Intent intent = new Intent(MainActivity.this, Main.class);
            startActivity(intent);
            finish();

        } else {
            Toast.makeText(MainActivity.this, "用户名或密码错误，或尚未注册", Toast.LENGTH_SHORT).show();
            Intent intent = new Intent(MainActivity.this, RegisterActivity.class);
            startActivity(intent);
        }
    }










}